package servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import dao.LoveMusicDao;
import entity.Music;
import entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


@WebServlet("/findLoveMusicServlet")
public class FindLoveMusicServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("application/json;charset=utf-8");


        String musicName = req.getParameter("loveMusicName");
        System.out.println("loveMusicName:"+musicName);

        User user = (User) req.getSession().getAttribute("user");
        int user_id = user.getId();

        LoveMusicDao loveMusicDao = new LoveMusicDao();
        List<Music> musics = new ArrayList<>();

        if(musicName != null) {
            musics = loveMusicDao.findLoveMusicBykeyAndUID(musicName,user_id);
        }else {
            musics = loveMusicDao.findLoveMusic(user_id);
        }

        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.writeValue(resp.getWriter(),musics);

    }

}